package gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import psim.Main;

/**
 * Trida Canvas dedi JPanel, pretezuje vykreslovani za ucelem
 * vykreslovani castic a vubec doublebufferingu
 *
 * @author Tomas Drimal
 */
public class Canvas extends JPanel implements Runnable {

    int x = 0;
    int y = 0;
    int x_old = 0;
    int y_old = 0;
    int Width = 0;
    int Height = 0;
    long cyklyOld = 0;
    ArrayList<Color> barvy = new ArrayList<Color>();
    BufferedImage imgTraces;
    Graphics2D g2D;

    public Canvas() {
        //pole barev castic
        barvy.add(Color.red);
        barvy.add(Color.white);
        barvy.add(Color.blue);
        barvy.add(Color.yellow);
        barvy.add(Color.gray);
        barvy.add(Color.green);
        barvy.add(Color.pink);
        barvy.add(Color.orange);
        barvy.add(Color.magenta);
        barvy.add(Color.cyan);
        init();

        // vybarveni obrazku bílou barvou
        g2D.setColor(Color.white);
        g2D.fillRect(0, 0, imgTraces.getWidth(), imgTraces.getHeight());
    }

    @Override
    public void paint(Graphics g) {
        //vymazani platna
        g.setColor(Color.white);
        g.clearRect(0, 0, getWidth(), getHeight());

        //cyklus for, ktery vypocita x a y polohy stredu castic
        for (int i = 0; i < Main.particles.size(); i++) {

            //x-ova a y-ova souradnice castice (dano stredem)
            x = (int) (Main.particles.get(i).x / Main.scale_x
                    * psim.Main.CLIENT_WIDTH_PX);
            y = (int) (Main.particles.get(i).y / Main.scale_y
                    * psim.Main.CLIENT_HEIGHT_PX);

            psim.Main.PtcsHist.get(i)[3] = x;
            psim.Main.PtcsHist.get(i)[4] = y;
        }
        // jestli je zadano vymazani drah castic
        if (Main.clrTraces) {
            g2D.setColor(Color.white);
            g2D.fillRect(0, 0, imgTraces.getWidth(), imgTraces.getHeight());
            Main.clrTraces = false;
        }
        //cyklus, ktery vykresluje historii drah castic
        for (int i = 0; i < Main.particles.size(); i++) {
            g2D.setColor(barvy.get((int) Main.particles.get(i).col));
            g2D.drawLine(Main.PtcsHist.get(i)[0], Main.PtcsHist.get(i)[1],
                    Main.PtcsHist.get(i)[3], Main.PtcsHist.get(i)[4]);
            }

        // jestli se maji zobrazit drahy castic
        if (Main.VTraces) {
            g.drawImage(imgTraces, 0, 0, this);
        }
//        Cyklus vykreslujici castice
        for (int i = 0; i < Main.particles.size(); i++) {
            //nastaveni barvy castice
            g.setColor(barvy.get((int) Main.particles.get(i).col));

            //samotne vykresleni
            g.fillOval(psim.Main.PtcsHist.get(i)[3] - psim.Main.PtcsHist.get(i)[2],
                    psim.Main.PtcsHist.get(i)[4] - psim.Main.PtcsHist.get(i)[2],
                    (int) Main.particles.get(i).d,
                    (int) Main.particles.get(i).d);
            psim.Main.PtcsHist.get(i)[0] = psim.Main.PtcsHist.get(i)[3];
            psim.Main.PtcsHist.get(i)[1] = psim.Main.PtcsHist.get(i)[4];
        }
        //získání poctu cyklu za ms
        gui.Gui.lCpsPMs.setText(Long.toString((Main.cykly - cyklyOld) / 50) + " cpms");
        cyklyOld = Main.cykly;
    }

    @SuppressWarnings("static-access")
    public void run() {
        while (true) {
            //prekresleni castic
            if (Main.simulation) {
                this.repaint();
            }
            try {
                //uspani vlakna prekreslovani na 50 ms,
                //aby se prekreslovalo 24/s
                Thread.sleep(50);
            } catch (InterruptedException ex) {
                Logger.getLogger(Canvas.class.getName()).
                        log(Level.SEVERE, null, ex);
                System.out.println("Nepodarilo se uspat");
            }

        }
    }
    /**
     * Inicializacni metoda, vola se pri vytvareni a
     * pri zmene velikosti platna
     */
    public void init() {
        this.imgTraces = new BufferedImage(psim.Main.CLIENT_WIDTH_PX,
                psim.Main.CLIENT_HEIGHT_PX, BufferedImage.TYPE_INT_RGB);
        this.g2D = imgTraces.createGraphics();
        Main.clrTraces = true;
    }
}
